home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / nmbd_subnetdb.c.z / nmbd_subnetdb.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  8.8 KB  |  318 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    NBT netbios routines and daemon - version 2
  5.    Copyright (C) Andrew Tridgell 1994-1998
  6.    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
  7.    Copyright (C) Jeremy Allison 1994-1998
  8.    
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2 of the License, or
  12.    (at your option) any later version.
  13.    
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.    
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.    
  23.    Revision History:
  24.  
  25. */
  26.  
  27. #include "includes.h"
  28. #include "smb.h"
  29.  
  30. extern int ClientNMB;
  31. extern int ClientDGRAM;
  32. extern int global_nmb_port;
  33.  
  34. extern int DEBUGLEVEL;
  35.  
  36. extern pstring myname;
  37. extern fstring myworkgroup;
  38. extern char **my_netbios_names;
  39. extern struct in_addr ipzero;
  40.  
  41. /* This is the broadcast subnets database. */
  42. struct subnet_record *subnetlist = NULL;
  43.  
  44. /* Extra subnets - keep these separate so enumeration code doesn't
  45.    run onto it by mistake. */
  46.  
  47. struct subnet_record *unicast_subnet = NULL;
  48. struct subnet_record *remote_broadcast_subnet = NULL;
  49. struct subnet_record *wins_server_subnet = NULL;
  50.  
  51. extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
  52.  
  53. /****************************************************************************
  54.   Add a subnet into the list.
  55.   **************************************************************************/
  56.  
  57. static void add_subnet(struct subnet_record *subrec)
  58. {
  59.   struct subnet_record *subrec2;
  60.  
  61.   if (!subnetlist)
  62.   {
  63.     subnetlist = subrec;
  64.     subrec->prev = NULL;
  65.     subrec->next = NULL;
  66.     return;
  67.   }
  68.  
  69.   for (subrec2 = subnetlist; subrec2->next; subrec2 = subrec2->next)
  70.     ;
  71.  
  72.   subrec2->next = subrec;
  73.   subrec->next = NULL;
  74.   subrec->prev = subrec2;
  75. }
  76.  
  77. /****************************************************************************
  78.   Create a subnet entry.
  79.   ****************************************************************************/
  80.  
  81. static struct subnet_record *make_subnet(char *name, enum subnet_type type,
  82.                                          struct in_addr myip, struct in_addr bcast_ip, 
  83.                                          struct in_addr mask_ip)
  84. {
  85.   struct subnet_record *subrec = NULL;
  86.   int nmb_sock, dgram_sock;
  87.  
  88.   /* Check if we are creating a non broadcast subnet - if so don't create
  89.      sockets.
  90.    */
  91.  
  92.   if(type != NORMAL_SUBNET)
  93.   {
  94.     nmb_sock = -1;
  95.     dgram_sock = -1;
  96.   }
  97.   else
  98.   {
  99.     /*
  100.      * Attempt to open the sockets on port 137/138 for this interface
  101.      * and bind them.
  102.      * Fail the subnet creation if this fails.
  103.      */
  104.  
  105.     if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
  106.     {
  107.       DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
  108. for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
  109.       return NULL;
  110.     }
  111.  
  112.     if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
  113.     {
  114.       DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
  115. for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
  116.       return NULL;
  117.     }
  118.  
  119.     /* Make sure we can broadcast from these sockets. */
  120.     set_socket_options(nmb_sock,"SO_BROADCAST");
  121.     set_socket_options(dgram_sock,"SO_BROADCAST");
  122.  
  123.   }
  124.  
  125.   subrec = (struct subnet_record *)malloc(sizeof(*subrec));
  126.   
  127.   if (!subrec) 
  128.   {
  129.     DEBUG(0,("make_subnet: malloc fail !\n"));
  130.     close(nmb_sock);
  131.     close(dgram_sock);
  132.     return(NULL);
  133.   }
  134.   
  135.   bzero((char *)subrec,sizeof(*subrec));
  136.   
  137.   if((subrec->subnet_name = strdup(name)) == NULL)
  138.   {
  139.     DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
  140.     close(nmb_sock);
  141.     close(dgram_sock);
  142.     free((char *)subrec);
  143.     return(NULL);
  144.   }
  145.  
  146.   DEBUG(2, ("making subnet name:%s ", name ));
  147.   DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
  148.   DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
  149.  
  150.   subrec->namelist_changed = False;
  151.   subrec->work_changed = False;
  152.  
  153.   subrec->bcast_ip = bcast_ip;
  154.   subrec->mask_ip  = mask_ip;
  155.   subrec->myip = myip;
  156.   subrec->type = type;
  157.   subrec->nmb_sock = nmb_sock;
  158.   subrec->dgram_sock = dgram_sock;
  159.   
  160.   return subrec;
  161. }
  162.  
  163. /****************************************************************************
  164.   Create subnet entries.
  165. **************************************************************************/
  166.  
  167. BOOL create_subnets(void)
  168. {    
  169.   int num_interfaces = iface_count();
  170.   int i;
  171.   struct in_addr unicast_ip;
  172.  
  173.   if(num_interfaces == 0)
  174.   {
  175.     DEBUG(0,("create_subnets: No local interfaces !\n"));
  176.     return False;
  177.   }
  178.  
  179.   /* 
  180.    * Create subnets from all the local interfaces and thread them onto
  181.    * the linked list. 
  182.    */
  183.  
  184.   for (i = 0 ; i < num_interfaces; i++)
  185.   {
  186.     struct subnet_record *subrec;
  187.     struct interface *iface = get_interface(i);
  188.  
  189.     if((subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
  190.                  iface->ip, iface->bcast,iface->nmask)) == NULL)
  191.       return False;
  192.     add_subnet(subrec);
  193.   }
  194.  
  195.   /* 
  196.    * If we have been configured to use a WINS server, then try and
  197.    * get the ip address of it here. If we are the WINS server then
  198.    * set the unicast subnet address to be the first of our own real
  199.    * addresses.
  200.    */
  201.  
  202.   if(*lp_wins_server())
  203.   {
  204.     struct in_addr real_wins_ip;
  205.     real_wins_ip = *interpret_addr2(lp_wins_server());
  206.  
  207.     if (!zero_ip(real_wins_ip))
  208.     {
  209.       unicast_ip = real_wins_ip;
  210.     }
  211.     else
  212.     {
  213.       /* The smb.conf's wins server parameter MUST be a host_name
  214.          or an ip_address. */
  215.       DEBUG(0,("invalid smb.conf parameter 'wins server'\n"));
  216.       return False;
  217.     }
  218.   } 
  219.   else if(lp_we_are_a_wins_server())
  220.   {
  221.     /* Pick the first interface ip address as the WINS server ip. */
  222.     unicast_ip = *iface_n_ip(0);
  223.   }
  224.   else
  225.   {
  226.     /* We should not be using a WINS server at all. Set the
  227.       ip address of the subnet to be zero. */
  228.     unicast_ip = ipzero;
  229.   }
  230.  
  231.   /*
  232.    * Create the unicast and remote broadcast subnets.
  233.    * Don't put these onto the linked list.
  234.    * The ip address of the unicast subnet is set to be
  235.    * the WINS server address, if it exists, or ipzero if not.
  236.    */
  237.  
  238.   unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
  239.                                  unicast_ip, unicast_ip, unicast_ip);
  240.  
  241.   remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
  242.                                          REMOTE_BROADCAST_SUBNET,
  243.                                          ipzero, ipzero, ipzero);
  244.  
  245.   if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
  246.     return False;
  247.  
  248.   /* 
  249.    * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
  250.    * the linked list.
  251.    */
  252.  
  253.   if (lp_we_are_a_wins_server())
  254.   {
  255.     if((wins_server_subnet = make_subnet("WINS_SERVER_SUBNET",
  256.                                        WINS_SERVER_SUBNET, 
  257.                                        ipzero, ipzero, ipzero)) == NULL)
  258.       return False;
  259.   }
  260.  
  261.   return True;
  262. }
  263.  
  264. /*******************************************************************
  265. Function to tell us if we can use the unicast subnet.
  266. ******************************************************************/
  267.  
  268. BOOL we_are_a_wins_client(void)
  269. {
  270.   static int cache_we_are_a_wins_client = -1;
  271.  
  272.   if(cache_we_are_a_wins_client == -1)
  273.     cache_we_are_a_wins_client = (ip_equal(ipzero, unicast_subnet->myip) ? 
  274.                                   False : True);
  275.  
  276.   return cache_we_are_a_wins_client;
  277. }
  278.  
  279. /*******************************************************************
  280. Access function used by NEXT_SUBNET_INCLUDING_UNICAST
  281. ******************************************************************/
  282.  
  283. struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec)
  284. {
  285.   if(subrec == unicast_subnet)
  286.     return NULL;
  287.   else if((subrec->next == NULL) && we_are_a_wins_client())
  288.     return unicast_subnet;
  289.   else
  290.     return subrec->next;
  291. }
  292.  
  293. /*******************************************************************
  294.  Access function used by retransmit_or_expire_response_records() in
  295.  nmbd_packets.c. Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>
  296.  Needed when we need to enumerate all the broadcast, unicast and
  297.  WINS subnets.
  298. ******************************************************************/
  299.  
  300. struct subnet_record *get_next_subnet_maybe_unicast_or_wins_server(struct subnet_record *subrec)
  301. {
  302.   if(subrec == unicast_subnet)
  303.   {
  304.     if(wins_server_subnet)
  305.       return wins_server_subnet;
  306.     else
  307.       return NULL;
  308.   }
  309.  
  310.   if(wins_server_subnet && subrec == wins_server_subnet)
  311.     return NULL;
  312.  
  313.   if((subrec->next == NULL) && we_are_a_wins_client())
  314.     return unicast_subnet;
  315.   else
  316.     return subrec->next;
  317. }
  318.